package main

import "fmt"

type Books struct { //结构体该包私有时小写，公有时大写
	Title   string `json:"title"` //转化为json字符串时该变量名需大写
	Author  string `json:"author"`
	Subject string `json:"subject"`
	book_id int    `json:""book_id` //小写则默认忽略
}

func (book Books) printall() { //为Books结构体增加方法
	fmt.Printf("Book title : %s\n", book.Title)
	fmt.Printf("Book author : %s\n", book.Author)
	fmt.Printf("Book subject : %s\n", book.Subject)
	fmt.Printf("Book book_id : %d\n", book.book_id)
}

//错误写法，无效函数，这么做无法改变结构体内参数
func (book Books) setname() {
	book.Title = "none"
}

//正确写法需使用指针
func (book *Books) setname2() {
	book.Title = "None"
}

//也可以写为单独函数
func setname3(book *Books) {
	book.Title = "None"
}
func main() {

	//两种初始化
	var Book1 Books
	Book2 := Books{"Pyhton", "author2", "Python Language", 00002}
	Book3 := Books{"C", "author3", "C Language", 00003}
	//var Book4 Books

	Book1.Title = "Go"
	Book1.Author = "author1"
	Book1.Subject = "Go Language"
	Book1.book_id = 00001

	//Book1.printall() //调用printall方法
	Book1.setname()
	Book2.setname2()
	setname3(&Book3)

	fmt.Println(Book1) //该结构体内元素在{}中输出
	fmt.Println(Book2)
	fmt.Println(Book3)
}
